package cn.iocoder.yudao.module.system.framework.examscore.template.impl;

import cn.iocoder.yudao.module.system.controller.admin.examscore.vo.ExamScoreImportExcelVO;
import cn.iocoder.yudao.module.system.framework.examscore.config.ExamScoreTemplateProperties;
import cn.iocoder.yudao.module.system.framework.examscore.template.ExamScoreTemplateDataSource;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 配置文件成绩导入模板数据源实现类
 *
 * @author 芋道源码
 */
@Component
public class ConfigExamScoreTemplateDataSource implements ExamScoreTemplateDataSource {

    @Resource
    private ExamScoreTemplateProperties examScoreTemplateProperties;

    @Override
    public List<ExamScoreImportExcelVO> getTemplateData() {
        List<ExamScoreTemplateProperties.TemplateData> configData = examScoreTemplateProperties.getData();
        if (CollectionUtils.isEmpty(configData)) {
            return new ArrayList<>();
        }

        List<ExamScoreImportExcelVO> result = new ArrayList<>();
        for (ExamScoreTemplateProperties.TemplateData data : configData) {
            ExamScoreImportExcelVO.ExamScoreImportExcelVOBuilder builder = ExamScoreImportExcelVO.builder()
                .studentId(data.getStudentId())
                .studentNo(data.getStudentNo())
                .studentName(data.getStudentName());

            // 处理各科成绩
            Map<String, String> scores = data.getScores();
            if (scores != null) {
                if (scores.containsKey("chinese")) {
                    builder.chineseScore(new BigDecimal(scores.get("chinese")));
                }
                if (scores.containsKey("math")) {
                    builder.mathScore(new BigDecimal(scores.get("math")));
                }
                if (scores.containsKey("english")) {
                    builder.englishScore(new BigDecimal(scores.get("english")));
                }
                if (scores.containsKey("physics")) {
                    builder.physicsScore(new BigDecimal(scores.get("physics")));
                }
                if (scores.containsKey("chemistry")) {
                    builder.chemistryScore(new BigDecimal(scores.get("chemistry")));
                }
                if (scores.containsKey("biology")) {
                    builder.biologyScore(new BigDecimal(scores.get("biology")));
                }
                if (scores.containsKey("politics")) {
                    builder.politicsScore(new BigDecimal(scores.get("politics")));
                }
                if (scores.containsKey("history")) {
                    builder.historyScore(new BigDecimal(scores.get("history")));
                }
                if (scores.containsKey("geography")) {
                    builder.geographyScore(new BigDecimal(scores.get("geography")));
                }
            }

            // 处理总分
            if (data.getTotalScore() != null) {
                builder.totalScore(new BigDecimal(data.getTotalScore()));
            }

            result.add(builder.build());
        }

        return result;
    }

    @Override
    public String getSourceType() {
        return "config";
    }
}